package org.codegallery.jpagal.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.codegallery.jpagal.entity.Customer;
import org.codegallery.jpagal.service.CustomerService;

public class CustomerServiceJpaImpl implements CustomerService {
	@PersistenceContext
	private EntityManager em;
	
	public Customer save(Customer customer) {
		if (customer.getId() == null) {
			em.persist(customer);
			return customer;
		} else {
			return em.merge(customer);
		}
	}

	public List<Customer> findByFirstname(String firstname) {
		TypedQuery query = em.createQuery("select c from Customer c where c.firstname = ?1", String.class);
	    query.setParameter(1, firstname);
	    return query.getResultList();
	}

}